import { LogHelper } from "../utils/logHelper"

/**
 * 被攻击事件
 */
export class AttacEventTask implements IEventTask{
  run(room: Room, event: EventItem): void {
    const eventData:EventData[EVENT_ATTACK] = event.data as EventData[EVENT_ATTACK];
    const creep = Game.getObjectById(eventData.targetId) as Creep | undefined;
    LogHelper.danger(`房间 [${room.name}] Creep ${creep?.name} 受到攻击`)
    if(creep && creep.my){
      const myRoom = Game.rooms[creep.memory.bornRoom];
      // 把附近的敌人都记录下来
      const enemies = creep.pos.findInRange(FIND_HOSTILE_CREEPS, 7);
      if(!myRoom.memory.enemys){
        myRoom.memory.enemys = new Array<Id<Creep>>;
      }
      for(const enemie of enemies){
        if(myRoom.memory.enemys.indexOf(enemie.id) === -1){
          myRoom.memory.enemys.push(enemie.id);
        }
      } 
    }
  }
}